﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySite.Timer;
using MySite.Helper;
using MySite.Enum;
using MySite.SinaWeiBo;
using System.Net.Json;
using MySite.Model;
using MySite.BLL;


namespace MySite.Timer
{
    public class SinaWeiBoTimer:ITimer
    {
        public string appKey = System.Configuration.ConfigurationManager.AppSettings["APPKEY"];
        public string appSecret = System.Configuration.ConfigurationManager.AppSettings["APPSECRET"];
        public string accessToken = System.Configuration.ConfigurationManager.AppSettings["AccessToken"];
        public string accessTokenSerect = System.Configuration.ConfigurationManager.AppSettings["AccessTokenSerect"];
        public OAuthSina oas;

        public SinaWeiBoTimer()
        {
            oas = new OAuthSina();
            oas.ConsumerKey = appKey;
            oas.ConsumerSecret = appSecret;
            oas.Token = accessToken;
            oas.TokenSecret = accessTokenSerect;
        }

        public override void GlobalTimer_ToDo(object state)
        {
            IList<StatusInfo> siList = new List<StatusInfo>();
            string url = "http://api.t.sina.com.cn/statuses/public_timeline.json?" + string.Format("source={0}&count={1}", appKey,20);
            string result = oas.OAuthWebRequest(HttpMethod.GET, url, null);

            var jsonArrayResult = JsonHelper.JsonParseToArrayCollection(result);

            if (jsonArrayResult == null)
            {
                var jsonObjectResult = JsonHelper.JsonParseToObjectCollection(result);
                foreach (JsonObject item in jsonObjectResult)
                {
                    item.GetValue();
                }
            }
            else
            {
                foreach (JsonObject item in jsonArrayResult)
                {
                   JsonObjectCollection joc =  item as JsonObjectCollection;
                   StatusInfo si = new StatusInfo();
                   foreach (var jocitem in joc)
                   {
                       switch (jocitem.Name)
                      {
                          case "created_at":
                              si.Created_at = Convert.ToDateTime(JsonHelper.FormatJsonDateTime(jocitem.GetValue().ToString()));
                              break;
                           case "text":
                              si.Text = Convert.ToString(jocitem.GetValue());
                              break;
                           case "id":
                              si.ID = Convert.ToInt64(jocitem.GetValue());
                              break;
                           case "source":
                              si.Source = Convert.ToString(jocitem.GetValue());
                              break;
                           case "favorited":
                              si.Favorited = Convert.ToBoolean(jocitem.GetValue());
                              break;
                           case "truncated":
                              si.Truncated = Convert.ToBoolean(jocitem.GetValue());
                              break;
                           case "in_reply_to_status_id":
                              Int64 In_reply_to_status_id;
                              Int64.TryParse(Convert.ToString(jocitem.GetValue()),out In_reply_to_status_id);
                              si.In_reply_to_status_id = In_reply_to_status_id;
                              break;
                           case "in_reply_to_user_id":
                               int In_reply_to_user_id;
                               Int32.TryParse(Convert.ToString(jocitem.GetValue()), out In_reply_to_user_id);
                              si.In_reply_to_user_id = In_reply_to_user_id;
                              break;
                           case "in_reply_to_screen_name":
                              si.In_reply_to_screen_name = Convert.ToString(jocitem.GetValue());
                              break;
                           case "thumbnail_pic":
                              si.Thumbnail_pic = Convert.ToString(jocitem.GetValue());
                              break;
                           case "bmiddle_pic":
                              si.Bmiddle_pic = Convert.ToString(jocitem.GetValue());
                              break;
                           case "original_pic":
                              si.Original_pic = Convert.ToString(jocitem.GetValue());
                              break;
                           case "user":
                             JsonObjectCollection user =  jocitem as JsonObjectCollection;
                             si.UID = Convert.ToInt64(user["id"].GetValue());
                             si.Screen_Name = Convert.ToString(user["screen_name"].GetValue());
                             break;
                      }
                   }

                   siList.Add(si);
                }
            }
            //删除今天以前的记录
            new Status().Delete(string.Format(" DateDiff('d',Created_at,Date())>0"));

            //记录到数据库
            new Status().UpdateStatus(siList);

        }
    }
}
